var mongogdb = require('./db');
var Post = require('./posts');
var mongoose = require('mongoose');
var sendMail = require('./mailer');
var getTime = require('./date');

function Comment(comment){
    this.articleId = comment.articleId;
    this.commentDetail = comment.commentDetail;
    this.mail = comment.mail;
    this.nickname = comment.nickname;
    this.loveNum = comment.loveNum;
    this.reply = "";
}
Comment.prototype= {
    save: function (callback) {
        this.time = getTime();
        var comment = {
            mail: this.mail,
            time: this.time.minute,
            nickname: this.nickname,
            reply: this.reply,
            loveNum: this.loveNum,
            articleId: this.articleId,
            commentDetail: this.commentDetail
        };

        mongogdb.open(function (err, db) {
            if (err) {
                return callback(err);
            }
            db.collection("comment", function (err, collection) {
                if (err) {
                    mongogdb.close();
                    return callback(err);
                }
                collection.insert(comment, {safe: true}, function (err) {
                    //mongogdb.close();
                    if (err) {
                        return callback(err);
                    }
                    sendMail('163','zskblognotify@163.com','ZhWy197531','979985773@qq.com','ZSKBlog新评论','新的评论快去看看吧  \n昵称: '+comment.nickname+' \n邮箱: '+comment.mail+' \ncontent: '+comment.commentDetail);
                    callback(null)

                })
            })
        })
    }
};

Comment.getAllByArticleId = function(articleId,callback){
    mongogdb.open(function (err, db) {
        if(err){
            return callback(err);
        }
        db.collection("comment",function(err,collection){
            if(err){
                return callback(err);
            }
            if(articleId){
                collection.find({articleId:articleId}).toArray(function(err,comments){
                    //mongogdb.close();
                    if(err){
                        return callback(err);
                    }
                    callback(null,comments);
                });
            }else{
                collection.find().toArray(function(err,comments){
                    //mongogdb.close();
                    if(err){
                        return callback(err);
                    }
                    callback(null,comments);
                });
            }

        });
    });
};
Comment.getAll = function(callback){
    mongogdb.open(function (err, db) {
        if(err){
            return callback(err);
        }
        db.collection("comment",function(err,collection){
            if(err){
                return callback(err);
            }
            collection.find().sort({time:-1}).toArray(function(err,comments){
                //mongogdb.close();
                console.log(comments);
                if(err){
                    return callback(err);
                }
                callback(null,comments);
            });
        });
    });
};
Comment.delete = function (_id, callback) {
    mongogdb.open(function (err, db) {
        if(err){
            return callback(err);
        }
        db.collection("comment",function(err,collection){
            if(err){
                return callback(err);
            }
            var realid = mongoose.Types.ObjectId(_id);
            collection.remove({_id:realid},function(err){
                //mongogdb.close();
                if(err){
                    console.log(err);
                    return callback(null,'failure');
                }
                callback(null,'success');
            });

        });
    });
};
Comment.calcCommentsNumForAId = function(callback){
    mongogdb.open(function(err,db){
        if(err){
            return callback(err);
        }
        db.collection('comment',function(err,collection){
            if(err){
                return callback(err);
            }
            console.log(collection.aggregate([{$group:{_id:"$articleId",num:{$sum:1}}}]).toArray());
            collection.aggregate([{$group:{_id:"$articleId",num:{$sum:1}}}]).toArray(function(err,commentsNum){
                //mongogdb.close();
                console.log('res: '+commentsNum);
                if(err){
                    return callback(err);
                }
                callback(null,commentsNum);
            });
        })
    })
};
module.exports = Comment;
